package com.cskaoyan.service.user;/*
 *  @Author tsuki
 *  @Date 2021/4/10
 *  @Description
 *
 */

import com.cskaoyan.bean.goods.GoodsExample;
import com.cskaoyan.bean.goods.GoodsProductExample;
import com.cskaoyan.bean.market.OrderExample;
import com.cskaoyan.bean.user.UserExample;
import com.cskaoyan.bo.system.AdminCreateBo;
import com.cskaoyan.mapper.*;
import com.cskaoyan.service.system.AdminService;
import com.cskaoyan.vo.system.AdminInfoVo;
import com.cskaoyan.vo.user.DashBoardVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

@Service
public class AuthServiceImpl implements AuthService {

    @Autowired
    AdminMapper adminMapper;

    @Autowired
    RoleMapper roleMapper;

    @Autowired
    PermissionMapper permissionMapper;

    @Autowired
    UserMapper userMapper;
    @Autowired
    GoodsMapper goodsMapper;
    @Autowired
    GoodsProductMapper goodsProductMapper;
    @Autowired
    OrderMapper orderMapper;

    @Override
    public DashBoardVo dashBoard() {
        DashBoardVo dashBoardVo = new DashBoardVo();
        UserExample userExample = new UserExample();
        long numUser = userMapper.countByExample(userExample);
        GoodsExample goodsExample = new GoodsExample();
        long numGoods = goodsMapper.countByExample(goodsExample);
        GoodsProductExample goodsProductExample = new GoodsProductExample();
        long numProduct = goodsProductMapper.countByExample(goodsProductExample);
        OrderExample orderExample = new OrderExample();
        long numOrder = orderMapper.countByExample(orderExample);
        dashBoardVo.setUserTotal(numUser);
        dashBoardVo.setGoodsTotal(numGoods);
        dashBoardVo.setOrderTotal(numOrder);
        dashBoardVo.setProductTotal(numProduct);
        return dashBoardVo;
    }

    @Override
    public AdminInfoVo getAdminInfo(String username) {
        AdminInfoVo adminInfoVo = new AdminInfoVo();
        AdminCreateBo adminCreateBo = adminMapper.selectTempInfo(username);
        adminInfoVo.setName(adminCreateBo.getUsername());
        adminInfoVo.setAvatar(adminCreateBo.getAvatar());

        List<String> roles = roleMapper.selectRoleByIds(adminCreateBo.getRoleIds());
        adminInfoVo.setRoles(roles);

        List<String> perms = permissionMapper.selectAPIByRids(adminCreateBo.getRoleIds());
        // List<String> perms = permissionMapper.selectPermByRids(adminCreateBo.getRoleIds());
        System.out.println(perms.toString());

        if (perms == null || perms.contains(null)) {
            perms.remove(null);
            perms.add("*");
            // perms = permissionMapper.selectSuperAPI();
        }
        adminInfoVo.setPerms(perms);

        return adminInfoVo;
    }
}
